<template>
  <div class="app-container">
    <myDialog
      :headTitle="headTitle"
      :key="keyIndex"
      :keyIndex.sync="keyIndex"
      ref="myDialog"
    />
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      label-width="68px"
    >
      <!-- <el-form-item label="系统模块" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入系统模块"
          clearable
          style="width: 240px;"
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="操作人员" prop="operName">
        <el-input
          v-model="queryParams.operName"
          placeholder="请输入操作人员"
          clearable
          style="width: 240px;"
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="businessType">
        <el-select
          v-model="queryParams.businessType"
          placeholder="操作类型"
          clearable
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="dict in typeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="操作状态"
          clearable
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item> -->
      <el-form-item label="操作时间">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="list"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            v-if="businessTypeFormat(scope.row) === '修改账户'"
            size="mini"
            type="text"
            @click="handleView(scope.row, scope.index, '修改账户')"
            >{{ businessTypeFormat(scope.row) }}</el-button
          >
          <el-button
            v-else-if="businessTypeFormat(scope.row) === '删除账户'"
            size="mini"
            type="text"
            @click="handleView(scope.row, scope.index, '删除账户')"
            >{{ businessTypeFormat(scope.row) }}</el-button
          >
          <el-button
            v-else-if="businessTypeFormat(scope.row) === '修改交易记录'"
            size="mini"
            type="text"
            @click="handleView(scope.row, scope.index, '修改交易记录')"
            >{{ businessTypeFormat(scope.row) }}</el-button
          >
          <el-button
            v-else-if="businessTypeFormat(scope.row) === '删除交易记录'"
            size="mini"
            type="text"
            @click="handleView(scope.row, scope.index, '删除交易记录')"
            >{{ businessTypeFormat(scope.row) }}</el-button
          >
          <span v-else>{{ businessTypeFormat(scope.row) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="对象"
        align="center"
        prop="title"
        :formatter="titleFormat"
      />
      <el-table-column label="操作人" align="center" prop="operName" />
      <el-table-column label="操作时间" align="center" prop="operTime" />
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {
  list,
  delOperlog,
  cleanOperlog,
  exportOperlog
} from "@/api/monitor/operlog";
import myDialog from "./components/dialog.vue";

export default {
  // name: "Operlog",
  components: {
    myDialog
  },
  data() {
    return {
      headTitle:'',
      keyIndex: 1,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      // 是否显示弹出层
      open: false,
      // 类型数据字典
      typeOptions: [],
      // 类型数据字典
      statusOptions: [],
      // 日期范围
      dateRange: [],
      // 表单参数
      form: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: undefined,
        operName: undefined,
        businessType: undefined,
        status: undefined
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_oper_type").then(response => {
      this.typeOptions = response.data;
    });
    this.getDicts("sys_common_status").then(response => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    /** 查询登录日志 */
    getList() {
      this.loading = true;
      list(this.addDateRange(this.queryParams, this.dateRange)).then(
        response => {
          this.list = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 操作日志状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 操作日志类型字典翻译
    typeFormat(row, column) {
      return this.selectDictLabel(this.typeOptions, row.businessType);
    },
    businessTypeFormat(row) {
      let tableName = row.tableName;
      let data = {
        修改: this.filtableName(row),
        删除: this.delfiltableName(row)
      };
      return data[this.typeFormat(row)] || this.typeFormat(row);
    },
    filtableName(row) {
      let tableName = row.tableName;
      if (tableName === "f_account_tag") {
        return "修改账户";
      } else if (tableName === "f_account_flow") {
        return "修改交易记录";
      } else {
        return this.typeFormat(row);
      }
    },
    delfiltableName(row) {
      let tableName = row.tableName;
      if (tableName === "f_account_tag") {
        return "删除账户";
      } else if (tableName === "f_account_flow") {
        return "删除交易记录";
      } else {
        return this.typeFormat(row);
      }
    },
    titleFormat(row, column) {
      console.log(row);
      let fil = {
        1: "往来账户",
        2: "资金账户",
        3: "标签设置"
      };
      if(row.tableName==='f_account_tag'){
        let operParam = JSON.parse(row.operParam);
        let category = operParam.category || "";
        return fil[category]
      }else if(row.tableName==='f_account_flow'){
        return row.title
      }else{
        return row.title||''
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.operId);
      this.multiple = !selection.length;
    },
    /** 详细按钮操作 */
    handleView(row,col,til) {
      // this.open = true;
      // this.form = row;
      this.headTitle=til
      this.$refs.myDialog.open(row)
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const operIds = row.operId || this.ids;
      this.$confirm(
        '是否确认删除日志编号为"' + operIds + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      )
        .then(function() {
          return delOperlog(operIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function() {});
    },
    /** 清空按钮操作 */
    handleClean() {
      this.$confirm("是否确认清空所有操作日志数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return cleanOperlog();
        })
        .then(() => {
          this.getList();
          this.msgSuccess("清空成功");
        })
        .catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有操作日志数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return exportOperlog(queryParams);
        })
        .then(response => {
          this.download(response.msg);
        })
        .catch(function() {});
    }
  }
};
</script>
